package band.wukong.practice.javase.core.tips;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

/**
 * Description: 按拼音排列字符串
 * Author: jsun
 * Date: 2009-10-26 22:05:36
 */
public class ChineseNameCollectionInOrder {
    public static void main(String[] args) {
        User user01 = new User("阿三", 18);
        User user02 = new User("段蕾", 19);
        User user03 = new User("长江", 11);
        User user04 = new User("版主", 25);

        Set<User> set = new TreeSet<User>();

        set.add(user01);
        set.add(user02);
        set.add(user03);
        set.add(user04);
        for (User user : set) {
            System.out.print(user.getName() + "---");
        }

    }

    static class User implements Comparable<User> {
        private String name;

        private int age;

        User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return this.name;
        }

        public int getAge() {
            return this.age;
        }

        public int compareTo(User other) {
            if (other == null) {
                return 1;
            }
            Comparator comparator = Collator.getInstance(Locale.CHINA);
            return comparator.compare(name, other.getName());
        }
    }
}

